package com.avamstudios;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class Messanger implements BeanPostProcessor {
	private MyFactory fabrikam;

	public Messanger(){
		System.out.println("Constructed");
	}
	
	public String echo(String msg){
		System.out.println(""+System.currentTimeMillis());
		System.out.println("üstadimin countu : "+ fabrikam.counter); 
		return msg;
	}

	public void setFabrikam(MyFactory myFactory) {
		fabrikam = myFactory;
		System.out.println("injected");
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String name)
			throws BeansException {
		try{
			Object value = bean.getClass().getMethod("get").invoke(bean);
			//Object[] params = new Object[1];
			value = value.toString().toUpperCase();
			bean.getClass().getMethod("set"+firstLetterCapital(name)).invoke(bean, value);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String name)
			throws BeansException {
		
		return null;
	}
	
	String firstLetterCapital(String name){
		return name.substring(0, 1).toUpperCase()+name.substring(1);
	}
	
	
}
